package elizascript;

import java.util.ArrayList;

/**
 * The ScriptInterop class provides a sort of interface between the script file and the Eliza Engine
 * It stores the information parsed from the script file
 * 
 * @author Adam
 *
 */

public class ScriptInterop {
	private String ScriptName;
	protected ArrayList<String> WelcomeMessages;
	protected ArrayList<String> QuitMessages;
	protected ArrayList<String> GenericMessages;
	protected ArrayList<KeywordObject> Keywords;
	protected ArrayList<SubstitutionRule> PresubstitutionRules;
	protected ArrayList<SubstitutionRule> PostsubstitutionRules;
	
	public boolean initialise(String scriptName) {
		ScriptName = scriptName;
		
		WelcomeMessages = new ArrayList<String>();
		QuitMessages = new ArrayList<String>();
		GenericMessages = new ArrayList<String>();
		PresubstitutionRules = new ArrayList<SubstitutionRule>();
		PostsubstitutionRules = new ArrayList<SubstitutionRule>();
		Keywords = new ArrayList<KeywordObject>();
		
		ScriptInterpreter interpreter = new ScriptInterpreter(this);
		
		
		if(!interpreter.interpretScript(ScriptName))
		{
			ScriptName = "";
			return false;
		}
		
		return true;
	}
	
	public boolean initialise() {
		return initialise("default.eli");
	}

	/**
	 * This function gets the KeywordObject for a String. Returns null if string isn't a keyword
	 * @param word - the keyword to be matched. Does not need to be lowercase'ed
	 * @return The appropriate KeywordObject object. Or null on fail to find keyword.
	 */
	public KeywordObject getKeywordObject(String word) {
		
		for(KeywordObject obj : Keywords) 
		{
			if(obj.Keyword.toLowerCase().equals(word.toLowerCase()))
			{
				return obj;
			}
		}
		return null;
	}
	
	/**
	 * Gets the priority of the specified keyword
	 * @param word - the keyword to be matched
	 * @return the priority, or -1 on fail to find keyword
	 */
	public int getKeywordPriority(String word) {
		
		for(KeywordObject obj : Keywords) 
		{
			if(obj.Keyword.toLowerCase().equals(word.toLowerCase()))
			{
				return obj.ID;
			}
		}
		return -1;
	}

	// returns the welcome messages
	public ArrayList<String> getWelcomeMessage() {
		return WelcomeMessages;
	}

	// returns a leaving message
	public ArrayList<String> getLeavingMessage() {
		return QuitMessages;
	}

	// returns a generic message
	public ArrayList<String> getGenericMessage() {
		return GenericMessages;
	}
	
	public ArrayList<SubstitutionRule> getPresubstitutionRule()
	{
		return PresubstitutionRules;
	}
	
	public ArrayList<SubstitutionRule> getPostsubstitutionRule()
	{
		return PostsubstitutionRules;
	}
}
